<template>
  <div>
    <FormPanel ref="from" :list="formList" @formChange="changeHandle" :isSubmitBtn="false" :collapse="false" labelWidth="110" />
    <div style="height:47px;"></div>
    <div
      :style="{
        position: 'absolute',
        left: 0,
        bottom: 0,
        right: 0,
        zIndex: 9,
        borderTop: '1px solid #e9e9e9',
        padding: '10px 20px',
        background: '#fff',
        textAlign: 'right'
      }"
    >
      <el-button @click="closeDrawer(1)">关 闭</el-button>
      <!-- <el-button @click="closeDrawer(2)" type="primary">保 存</el-button> -->
      <multiuse-button type="primary" :click="closeDrawer.bind(this, 2)">保 存</multiuse-button>
    </div>
  </div>
</template>
<script>
import { dictionary } from '@/utils/dictMixin';
import { isArcfox } from '@/api/cms-api/origin/commonDealerType';
import { getList, getDetail, postSave } from '@/api/omSale/vehicleParameters/capitalVerification';
export default {
  data() {
    this.saveData = null;
    return {
      orgList: [],
      deptList: [
        { text: '南方销售办公室', value: '0001' },
        { text: '大客户部', value: 'S000' },
        { text: '华北事业部', value: 'S001' },
        { text: '华东事业部', value: 'S002' },
        { text: '华中事业部', value: 'S003' },
        { text: '华南事业部', value: 'S004' },
        { text: '西南事业部', value: 'S005' },
        { text: '西北事业部', value: 'S006' }
      ],
      userList: [
        { text: '专票', value: 0 },
        { text: '机动车发票', value: 2 },
        { text: '普票', value: 12 }
      ],
      priceList: [
        { text: '立即应付的到期净值', value: '0001' },
        { text: '14天之内2%的现金折扣，30天之内到期净值', value: '0002' },
        { text: '14天之内的3%的现金折扣，20天之内2%的现金折扣，30天之内到期净值', value: '0003' },
        { text: '立即应付的到期净值，基准日期为当月结束', value: '0004' },
        { text: '立即应付的到期净值，基准日期为下月10', value: '0005' }
      ],
      accountList: [
        { text: '国内客户', value: '01' },
        { text: '国外客户', value: '02' }
      ],
      taxList: [
        {
          text: '免税',
          value: '0'
        },
        {
          text: '必须上税',
          value: '1'
        }
      ],
      formList: this.createdFrom()
    };
  },
  props: ['data', 'dealerCode'],
  created() {
    this.formList.find(x => x.fieldName == 'beneficiary').initialValue = this.dealerCode;
    this.formList.find(x => x.fieldName == 'payer').initialValue = this.dealerCode;
    this.formList.find(x => x.fieldName == 'deliveryParty').initialValue = this.dealerCode;
    if (isArcfox()) {
      this.orgList = [
        { text: '北京蓝谷极狐投资管理有限公司销售组织', value: 1012 },
        { text: '北京蓝谷极狐投资管理有限公司配件销售组织', value: '112A' }
      ];
    } else {
      this.orgList = [
        { text: '北京新能源汽车股份有限公司青岛分公司', value: 1051 },
        { text: '北京新能源汽车常州有限公司', value: 1052 },
        { text: '北京新能源汽车营销有限公司', value: 2000 },
        { text: '青岛卫蓝销售公司', value: 2001 },
        { text: '北京新能源汽车营销有限公司配件销售组织', value: '200A' }
      ];
    }
    if (this.data) {
      for (let i in this.data) {
        this.formList.map(x => {
          if (i == x.fieldName) {
            x.initialValue = this.data[i];
          }
        });
      }
    }
  },
  mixins: [dictionary],
  methods: {
    createdFrom() {
      let orgList = [],
        deptList = [
          { text: '南方销售办公室', value: '0001' },
          { text: '大客户部', value: 'S000' },
          { text: '华北事业部', value: 'S001' },
          { text: '华东事业部', value: 'S002' },
          { text: '华中事业部', value: 'S003' },
          { text: '华南事业部', value: 'S004' },
          { text: '西南事业部', value: 'S005' },
          { text: '西北事业部', value: 'S006' }
        ],
        userList = [
          { text: '专票', value: '0' },
          { text: '机动车发票', value: '2' },
          { text: '普票', value: '12' }
        ],
        priceList = [
          { text: '立即应付的到期净值', value: '0001' },
          { text: '14天之内2%的现金折扣，30天之内到期净值', value: '0002' },
          { text: '14天之内的3%的现金折扣，20天之内2%的现金折扣，30天之内到期净值', value: '0003' },
          { text: '立即应付的到期净值，基准日期为当月结束', value: '0004' },
          { text: '立即应付的到期净值，基准日期为下月10', value: '0005' }
        ],
        accountList = [
          { text: '国内客户', value: '01' },
          { text: '国外客户', value: '02' }
        ],
        taxList = [
          {
            text: '免税',
            value: '0'
          },
          {
            text: '必须上税',
            value: '1'
          }
        ],
        Trade = [
          {
            text: '成本和运费',
            value: 'CFR'
          },
          {
            text: '成本,保险&运费',
            value: 'CIF'
          },
          {
            text: '运费和保险费已付',
            value: 'CIP'
          },
          {
            text: '运费已付',
            value: 'CPT'
          },
          {
            text: '边境交货',
            value: 'DAF'
          },
          {
            text: '完税后交货价',
            value: 'DDP'
          },
          {
            text: '未完税交货价',
            value: 'DDU'
          },
          {
            text: '码头交货(完税)',
            value: 'DEQ'
          },
          {
            text: '船上交货',
            value: 'DES'
          },
          {
            text: '工厂交货',
            value: 'EXW'
          },
          {
            text: '免靠船',
            value: 'FAS'
          },
          {
            text: '免运费',
            value: 'FCA'
          },
          {
            text: '仓库交货',
            value: 'FH'
          },
          {
            text: '离岸价',
            value: 'FOB'
          },
          {
            text: '不免费',
            value: 'UN'
          }
        ];
      if (isArcfox()) {
        orgList = [
          { text: '北京蓝谷极狐投资管理有限公司销售组织', value: 1012 },
          { text: '北京蓝谷极狐投资管理有限公司配件销售组织', value: '112A' }
        ];
      } else {
        orgList = [
          { text: '北京新能源汽车股份有限公司青岛分公司', value: 1051 },
          { text: '北京新能源汽车常州有限公司', value: 1052 },
          { text: '北京新能源汽车营销有限公司', value: 2000 },
          { text: '青岛卫蓝销售公司', value: 2001 },
          { text: '北京新能源汽车营销有限公司配件销售组织', value: '200A' }
        ];
      }
      return [
        {
          type: 'SELECT',
          label: '销售组织',
          fieldName: 'salesOrgId',
          placeholder: '请选择',
          itemList: orgList || [],
          rules: [{ required: true, message: '请选择', trigger: 'change' }]
        },
        // {
        //   type: 'INPUT',
        //   label: '销售组织名称',
        //   fieldName: 'salesOrgName',
        //   placeholder: '请选择',
        //   rules: [{ required: true, message: '请选择', trigger: 'blur' }]
        // },
        // {
        //   type: 'INPUT',
        //   label: '销售组织代码',
        //   fieldName: 'salesOrgId',
        //   placeholder: '请选择',
        //   rules: [{ required: true, message: '请选择', trigger: 'blur' }]
        // },
        {
          type: 'SELECT',
          label: '销售部门',
          fieldName: 'salesDeptId',
          placeholder: '请选择',
          itemList: deptList || []
        },
        // {
        //   type: 'INPUT',
        //   label: '销售部门名称',
        //   fieldName: 'salesDeptName',
        //   placeholder: '请选择',
        //   rules: [{ required: true, message: '请选择', trigger: 'blur' }]
        // },
        // {
        //   type: 'INPUT',
        //   label: '销售部门代码',
        //   fieldName: 'salesDeptId',
        //   placeholder: '请选择',
        //   rules: [{ required: true, message: '请选择', trigger: 'blur' }]
        // },
        {
          type: 'INPUT',
          label: '销售组',
          fieldName: 'salesTeamName',
          placeholder: '请输入'
        },
        {
          type: 'SELECT',
          label: '客户组',
          fieldName: 'customerTeamId',
          placeholder: '请选择',
          itemList: userList || []
        },
        {
          type: 'SELECT',
          label: '国际贸易术语1',
          fieldName: 'internationalTradeTermsFirst',
          itemList: Trade || [],
          placeholder: '请选择'
          // rules: [{ required: true, message: '请选择', trigger: 'change' }]
        },
        {
          type: 'SELECT',
          label: '国际贸易术语2',
          fieldName: 'internationalTradeTermsSecond',
          itemList: Trade || [],
          placeholder: '请选择'
          // rules: [{ required: true, message: '请选择', trigger: 'change' }]
        },
        {
          type: 'SELECT',
          label: '付款条件',
          fieldName: 'paymentTerms',
          placeholder: '请选择',
          itemList: priceList || [],
          rules: [{ required: true, message: '请选择', trigger: 'change' }]
        },
        {
          type: 'SELECT',
          label: '账户分配组',
          fieldName: 'accountAllocationGroup',
          placeholder: '请选择',
          itemList: accountList || []
        },
        {
          type: 'SELECT',
          label: '税分类',
          fieldName: 'taxClassification',
          placeholder: '请选择',
          itemList: taxList || [],
          rules: [{ required: true, message: '请选择', trigger: 'change' }]
        },
        {
          type: 'INPUT',
          label: '收票方',
          fieldName: 'beneficiary',
          placeholder: '请输入'
        },
        {
          type: 'INPUT',
          label: '付款方',
          fieldName: 'payer',
          placeholder: '请输入'
        },
        {
          type: 'INPUT',
          label: '送达方',
          fieldName: 'deliveryParty',
          placeholder: '请输入'
        }
      ];
    },
    changeValue(value, arr) {
      if (arr.length > 0) {
        let val = '';
        arr.map(x => {
          if (x.value == value) {
            val = x.text;
          }
        });
        return val;
      }
    },
    changeHandle(val) {
      this.saveData = val;
    },
    async closeDrawer(type) {
      if (type == 1) {
        this.$emit('close');
      } else {
        this.$refs.from.SUBMIT_FORM();
        if (this.saveData) {
          let json = this.saveData;
          json.salesOrgName = this.changeValue(json.salesOrgId, this.orgList);
          json.salesDeptName = this.changeValue(json.salesDeptId, this.deptList);
          if (this.data) {
            json._uid = this.data._uid;
          }
          this.$emit('close', json);
        }
      }
    }
  }
};
</script>
